<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use app\index\model\member;
use app\index\model\sms_log;

class Login extends Controller
{
	public function __construct(){
        parent::__construct();
    }

	public function login()
    {
        if (Request::instance()->isPost()) {
            $member = new member();
            $condition = array();
            $condition['mobile']       = input('put.mobile');
            $condition['password']     = MD5(input('put.password'));
            $member_info = $member->getMemberInfo($condition);
            if (empty($member_info)) {
                $data = [
                    'msg'   =>  '账号或者密码错误！',
                    'stats' =>  false
                ];
            }else{
                Session::set('member_id',$member_info['member_id']);
                $data = [
                    'msg'   =>  '登录成功！',
                    'stats' =>  true
                ];
            }
            return $data;
        }else{
            return $this->fetch();
        }
    }

    public function register()
    {
        if (Request::instance()->isPost()) {
            $member = array();
            $member['mobile']       = input('put.mobile');
            $member['reg_time']     = time();
            $member['password']     = MD5(input('put.password'));
            $member['member_name']  = input('put.member_name');
            $mobilecode = input('put.mobilecode');
            $mobile = input('put.mobile');
            $type = input('put.type');
            $is_code = $this->smsRegister($mobilecode,$mobile,$type);
            if (!$is_code['stats']) {
                $data = [
                    'msg'   =>  $is_code['msg'],
                    'stats' =>  false
                ];
                return json($data);
            }
            if ($member['member_name'] == false) {
                $data = [
                    'msg'   =>  '请填写用户名称！',
                    'stats' =>  false
                ];
                return json($data);
            }
            //查看数据库是否存在手机号
            $presence = Db::name('member')->where(array('mobile'=>input('put.mobile')))->find();
            if(!empty($presence)){
                $data = [
                    'msg'   =>  '手机号码已注册！',
                    'stats' =>  false
                ];
                return json($data);
            }
            $insert_id = $this->register_Add($member);
            if ($insert_id) {
                $data = [
                    'msg'   =>  '注册成功',
                    'stats' =>  true
                ];
                return json($data);
            }else{
                $data = [
                    'msg'   =>  '注册失败',
                    'stats' =>  false
                ];
                return json($data);
            }
        }else{
            return $this->fetch();
        }
    }

    //判断验证码
    private function smsRegister($mobilecode,$mobile,$type)
    {
        $condition = array();
        $condition['log_captcha'] = $mobilecode;
        $condition['log_phone']   = $mobile;
        $condition['log_type']    = $type;
        $sms_log = Db::name('sms_log')->where($condition)->find();
        if (!empty($sms_log) && $sms_log['add_time']+60*60 < time()) {
            $data = [
                'msg'   =>  '验证码已过期！',
                'stats' =>  false
            ];
            return $data;
        }elseif ($sms_log['log_captcha'] == $mobilecode) {
            $data = [
                'msg'   =>  '验证码正确！',
                'stats' =>  true
            ];
            return $data;
        }else{
            $data = [
                'msg'   =>  '验证码错误！',
                'stats' =>  false
            ];
            return $data;
        }
    }
    //发送验证码
    public function sendCaptcha()
    {
        $type = input('put.type');
        $mobile = input('put.mobile');
        //获取验证码的时间（是否在1分中以内）同一手机，同一ip不能请求过多（1分中之内）
        $sms_log = $this->ipCaptcha($type);
        if (!empty($sms_log) && $sms_log['add_time']+60*60 < time()) {
            $data = [
                'msg'   =>  '一分钟之内请求时间过快！',
                'stats' =>  false
            ];
            return json($data);
        }
        $sms_code = array();
        $sms_code['log_captcha'] = rand(100000, 999999);
        $sms_code['log_ip'] = getIp();
        $sms_code['add_time'] = time();
        $sms_code['log_phone'] = $mobile;
        $sms_code['log_type'] = $type;
        $sms_code['member_name'] = input('put.member_name');
        if ($sms_code['log_phone'] == false) {
            $data = [
                'msg'   =>  '请填写手机号码！',
                'stats' =>  false
            ];
            return json($data);
        }
        $insert_id = Db::name('sms_log')->insert($sms_code);
        if(empty($insert_id)){
            $data = [
                'msg'   =>  '验证码发送失败！',
                'stats' =>  false
            ];
            return json($data);
        }else{
            /*
            请求短信接口api
            */
            $data = [
                'msg'   =>  '验证码发送成功！',
                'stats' =>   true
            ];
            return json($data);
        }
    }
    /**
     * 按IP查询手机验证码
     * @param string $log_type
     * @return array
     */
    private function ipCaptcha($type = '')
    {
        $condition = array();
        $condition['log_ip'] = getIp();
        $log_type = $type;
        if ($log_type > 0) {
            $condition['log_type'] = $log_type;//短信类型:1为注册,2为登录,3为找回密码
        }
        $model_sms_log = new sms_log();
        $sms_log = $model_sms_log->getSmsInfo($condition);
        return $sms_log;
    }

    /**
     * 添加会员
     * @param array $member_info
     */
    private function register_Add($member_info)
    {
        $member_list = array();
        $member_list['email'] = '';
        $member_list['password'] = $member_info['password'];
        $member_list['reg_time'] = $member_info['reg_time'];
        $member_list['last_ip']  = getIp();
        $member_list['mobile']   = $member_info['mobile'];
        $member_list['member_name']   = $member_info['member_name'];
        if ($member_info['mobile']) {
            $member_list['mobile_validated'] = 1;
        }
        $is_pass = Db::name('member')->where(array('member_name'=>$member_info['member_name']))->find();
        if (!empty($is_pass)) {
            return false;
        }
        $member = new member();
        $result = $member->getMemberInsert($member_list);
        return $result;
    }

    public function isMemberKeyName()
    {
        $member_name = input('put.member_name');
        $member = new member();
        $condition = array();
        $condition['member_name'] = $member_name;
        $member_list = $member->getMemberInfo($condition);
        if (!empty($member_list)) {
            $data = [
                'msg'   =>  '用户名已存在！',
                'stats' =>  true
            ];
        }else{
            $data = [
                'msg'   =>  '用户名可以申请',
                'stats' =>  false
            ];
        }
        return $data;
    }
}